package com.hfb.mashibing.alip8.juc.c020_01_interview.A1B2C3;

import java.util.concurrent.locks.LockSupport;

/**
 *  互联网一线大厂面试题：
 *      徒手写出：2个不同的线程，交替输出A1B2C3。。。
 *
 * 线程排序问题
 * 思路：利用 LockSupport，分别依次遍历字母队列和数字队列
 */
public class A1B2C3LockSupport {

    private static char[] charsLetter = "ABCDEFGHI".toCharArray();
    private static char[] charsNumber = "123456789".toCharArray();

    private static Thread t1, t2;


    public static void main(String[] args) {
        t1 = new Thread(()->{
            for (int i = 0; i < charsLetter.length; i++) {
                LockSupport.unpark(t2);
                System.out.print(charsLetter[i]);
                // 唤醒等待线程，只有2个线程，故这里唤醒的是另一个线程
                LockSupport.park();
            }
        });

        t2 = new Thread(()->{
            for (int i = 0; i < charsNumber.length; i++) {
                LockSupport.park();

                System.out.print(charsNumber[i]);

                LockSupport.unpark(t1);
            }
            System.out.println();
        });

        t1.start();
        t2.start();
    }

}
